﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    /// <summary>
    /// 时间帮助类
    /// </summary>
    public class TimeHelper
    {
        /// <summary>
        /// 是否介于时间段
        /// </summary>
        /// <param name="compareTime">比较时间</param>
        /// <param name="startHour">开始小时</param>
        /// <param name="startMinute">开始分钟</param>
        /// <param name="endHour">结束小时</param>
        /// <param name="endMinute">结束分钟</param>
        /// <returns>true:比较时间在该时间段内 false:比较时间不在该时间段内</returns>
        public static bool IsBetweenTime(DateTime compareTime, int startHour, int startMinute, int endHour, int endMinute)
        {
            int currentHour = compareTime.Hour;
            int currentMinute = compareTime.Minute;
            if (!(currentHour >= startHour && currentHour <= endHour))
            {
                return false;
            }
            if (currentHour == startHour)
            {
                //比较分钟
                if (startMinute > currentMinute)
                {
                    return false;
                }
            }
            if (currentHour == endHour)
            {
                if (currentMinute > endMinute)
                {
                    return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 是否介于时间段
        /// </summary>
        /// <param name="compareTime">比较时间</param>
        /// <param name="startHour">开始小时</param>
        /// <param name="startMinute">开始分钟</param>
        /// <param name="endHour">结束小时</param>
        /// <param name="endMinute">结束分钟</param>
        /// <returns>true:比较时间在该时间段内 false:比较时间不在该时间段内</returns>
        public static bool IsBetweenTime(DateTime compareTime, string startHour, string startMinute, string endHour, string endMinute)
        {
            int sHour = 0, sMinute = 0, eHour = 0, eMinute = 0;
            if (!int.TryParse(startHour, out sHour) || !int.TryParse(startMinute, out sMinute) || !int.TryParse(endHour, out eHour) || !int.TryParse(endMinute, out eMinute))
            {
                return false;
            }
            return IsBetweenTime(compareTime, sHour, eHour, eHour, eMinute);
        }
        /// <summary>
        /// 当前时间是否介于时间段
        /// </summary>
        /// <param name="startHour">开始小时</param>
        /// <param name="startMinute">开始分钟</param>
        /// <param name="endHour">结束小时</param>
        /// <param name="endMinute">结束分钟</param>
        /// <returns>true:比较时间在该时间段内 false:比较时间不在该时间段内</returns>
        public static bool NowIsBetweenTime(string startHour, string startMinute, string endHour, string endMinute)
        {
            return IsBetweenTime(DateTime.Now, startHour, startMinute, endHour, endMinute);
        }
        /// <summary>
        /// 当前时间是否介于时间段
        /// </summary>
        /// <param name="startHour">开始小时</param>
        /// <param name="startMinute">开始分钟</param>
        /// <param name="endHour">结束小时</param>
        /// <param name="endMinute">结束分钟</param>
        /// <returns>true:比较时间在该时间段内 false:比较时间不在该时间段内</returns>
        public static bool NowIsBetweenTime(int startHour, int startMinute, int endHour, int endMinute)
        {
            return IsBetweenTime(DateTime.Now, startHour, startMinute, endHour, endMinute);
        }
    }
}
